<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="messageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="消息ID" align="center" prop="messageId" />
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="内容"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="消息类型" align="center" prop="messageType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_message_type"
            :value="scope.row.messageType"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_message_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="发送时间"
        align="center"
        prop="sendTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.sendTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleMarkReadSingle(scope.row)"
            >标为已读</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:message:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看消息详情对话框 -->
    <el-dialog
      :title="'查看消息详情'"
      :visible.sync="viewOpen"
      width="600px"
      append-to-body
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="消息标题">
          <span>{{ form.title }}</span>
        </el-form-item>
        <el-form-item label="消息类型">
          <dict-tag
            :options="dict.type.sys_message_type"
            :value="form.messageType"
          />
        </el-form-item>
        <el-form-item label="消息内容">
          <div style="white-space: pre-wrap">{{ form.content }}</div>
        </el-form-item>
        <el-form-item label="发送时间">
          <span>{{ parseTime(form.sendTime) }}</span>
        </el-form-item>
        <el-form-item label="状态">
          <dict-tag
            :options="dict.type.sys_message_status"
            :value="form.status"
          />
        </el-form-item>
        <el-form-item v-if="form.status === '1'" label="阅读时间">
          <span>{{ parseTime(form.readTime) }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="form.status === '0'"
          type="primary"
          @click="handleMarkReadConfirm"
          >标记为已读</el-button
        >
        <el-button @click="viewOpen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMessage,
  getMessage,
  delMessage,
  markAsRead,
} from "@/api/system/message";

export default {
  name: "Message",
  dicts: ["sys_message_type", "sys_message_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息通知表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        messageType: null,
        status: null,
      },
      // 表单参数
      form: {},
    };
  },
  mounted() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询消息通知列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then((response) => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        messageId: null,
        title: null,
        content: null,
        messageType: null,
        receiverId: null,
        receiverType: null,
        status: "0",
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.messageId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const messageId = row.messageId || this.ids;
      getMessage(messageId).then((response) => {
        this.form = response.data;
        this.viewOpen = true;
        // 如果消息未读，打开时自动标记为已读
        if (this.form.status === "0") {
          this.handleMarkReadConfirm();
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const messageIds = row.messageId || this.ids;
      this.$modal
        .confirm('是否确认删除消息ID为"' + messageIds + '"的数据项?')
        .then(() => {
          return delMessage(messageIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 标记全部选中的消息为已读 */
    handleMarkRead() {
      if (this.ids.length === 0) {
        this.$modal.msgError("请先选择需要标记的消息");
        return;
      }

      const promises = this.ids.map((id) => markAsRead(id));
      Promise.all(promises).then(() => {
        this.getList();
        this.$modal.msgSuccess("标记已读成功");
      });
    },
    /** 标记单条消息为已读 */
    handleMarkReadSingle(row) {
      markAsRead(row.messageId).then(() => {
        this.getList();
        this.$modal.msgSuccess("标记已读成功");
      });
    },
    /** 查看详情时标记消息已读 */
    handleMarkReadConfirm() {
      markAsRead(this.form.messageId).then(() => {
        this.form.status = "1";
        this.form.readTime = new Date();
        this.getList();
      });
    },
  },
};
</script>
